package com.harvest.scientific.ladder.dialog;

import android.annotation.SuppressLint;
import android.app.Dialog;
import android.content.Context;
import android.content.res.ColorStateList;
import android.graphics.Color;
import android.graphics.Typeface;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;
import android.widget.FrameLayout;
import android.widget.TextView;


import com.google.android.material.shape.MaterialShapeDrawable;
import com.harvest.scientific.ladder.R;

import androidx.annotation.NonNull;
import androidx.lifecycle.OnLifecycleEvent;

import static com.harvest.scientific.ladder.ext.CommonHelper.dp2px;
import static com.harvest.scientific.ladder.ext.CommonHelper.getScreenWidth;

/**
 * @author keke
 * 统一弹窗dialog
 */
public class SimpleDialog extends Dialog {
    private int resourceId;
    private Context context;
    private View.OnClickListener cancelListener;
    private View.OnClickListener okListener;
    private CharSequence title;
    private CharSequence message;
    private CharSequence rightText;
    private CharSequence leftText;
    private CharSequence centerBtnText;
    private TextView textMessageView;
    private TextView textTitle;
    private TextView mBtnRight;
    private TextView mBtnLeft;
    private TextView mBtnCenter;
    private View mLayoutDouble;
    private int mBtnLeftTextColor;
    private int mBtnRightTextColor;
    private int mBtnCenterTextColor;
    private int mBtnCenterTextStyle = -1;
    private int mTitleGravity;
    private int mMessageGravity;
    private int mMessageTextColor;
    private int mMessageTextSize;
    private int mMessageTextLines;
    private FrameLayout rightCloseContainer;

    private boolean isSingleBnt = false;
    private boolean mTextBold;

    private int dialogWidth = 0, dialogHeight = 0;

    /**
     *
     */
    public SimpleDialog(@NonNull Context context) {
        super(context, R.style.dialogStyle);
        this.context = context;
        resourceId = R.layout.dialog_norml;
        //统一弹窗默认边距40px
        setDialogWidth((int) (getScreenWidth() - 2 * dp2px(40f)));
    }

    public SimpleDialog(@NonNull Context context, int resourceId) {
        this(context);
        this.resourceId = resourceId;
    }



    @SuppressLint("WrongConstant")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(resourceId);

        WindowManager.LayoutParams Params = getWindow().getAttributes();
        if (dialogWidth != 0) {
            Params.width = dialogWidth;
        }
        if (dialogHeight != 0) {
            Params.height = dialogHeight;
        }
        getWindow().setAttributes(Params);

        mBtnLeft = findViewById(R.id.close_button);
        mBtnRight = findViewById(R.id.confirm_button);
        textMessageView = findViewById(R.id.tv_message);
        textTitle = findViewById(R.id.tv_title);
        rightCloseContainer = findViewById(R.id.top_close_container);
        if (textTitle != null) {
            if (!TextUtils.isEmpty(title)) {
                textTitle.setText(title);
                textTitle.setVisibility(View.VISIBLE);
            }
            if (mTitleGravity != 0) {
                textTitle.setGravity(mTitleGravity);
            }
        }
        if (textMessageView != null) {
            if (TextUtils.isEmpty(message)) {
                textMessageView.setVisibility(View.GONE);
            } else {
                if(TextUtils.isEmpty(title)){
                    textMessageView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 16);
                    textMessageView.setTextColor(Color.parseColor("#333333"));
                }
                textMessageView.setText(message);
                textMessageView.setVisibility(View.VISIBLE);
                if (mMessageTextColor != 0) {
                    textMessageView.setTextColor(mMessageTextColor);
                }
                if (mMessageTextSize > 0) {
                    textMessageView.setTextSize(TypedValue.COMPLEX_UNIT_SP, mMessageTextSize);
                }
                if (mMessageTextLines > 0) {
                    textMessageView.setMaxLines(mMessageTextLines);
                    textMessageView.setEllipsize(TextUtils.TruncateAt.END);
                }
                if (mMessageGravity != 0) {
                    textMessageView.setGravity(mMessageGravity);
                }
                if (mTextBold) {
                    textMessageView.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));
                }
            }
        }
        if (mBtnLeft != null) {
            if (!TextUtils.isEmpty(leftText)) {
                mBtnLeft.setText(leftText);
            }
            if (mBtnLeftTextColor != 0) {
                mBtnLeft.setTextColor(mBtnLeftTextColor);
            }
            if (cancelListener != null) {
                mBtnLeft.setOnClickListener(cancelListener);
            } else {
                mBtnLeft.setOnClickListener(view -> dismiss());
            }
            MaterialShapeDrawable drawable = new MaterialShapeDrawable();
            drawable.setFillColor(ColorStateList.valueOf(Color.parseColor("#989ecf")));
            drawable.setCornerSize(dp2px(22));
            mBtnLeft.setBackground(drawable);
        }
        if (mBtnRight != null) {
            if (!TextUtils.isEmpty(rightText)) {
                mBtnRight.setText(rightText);
            }
            if (mBtnRightTextColor != 0) {
                mBtnRight.setTextColor(mBtnRightTextColor);
            }
            mBtnRight.setOnClickListener(view -> okListener.onClick(view));
            MaterialShapeDrawable drawable = new MaterialShapeDrawable();
            drawable.setFillColor(ColorStateList.valueOf(Color.parseColor("#28359B")));
            drawable.setCornerSize(dp2px(22));
            mBtnRight.setBackground(drawable);
        }
        if (mBtnCenter != null) {
            if (mBtnCenterTextColor != 0) {
                mBtnCenter.setTextColor(mBtnCenterTextColor);
            }
            if (mBtnCenterTextStyle > -1) {
                mBtnCenter.setTypeface(Typeface.defaultFromStyle(mBtnCenterTextStyle));
            }
            if (!TextUtils.isEmpty(centerBtnText)) {
                mBtnCenter.setText(centerBtnText);
            }
            mBtnCenter.setOnClickListener(view -> dismiss());
        }
        if (isSingleBnt) {
            if (mLayoutDouble != null) {
                mLayoutDouble.setVisibility(View.GONE);
            }
            if (mBtnCenter != null) {
                mBtnCenter.setVisibility(View.VISIBLE);
                if (okListener != null) {
                    mBtnCenter.setOnClickListener(view -> okListener.onClick(view));
                }
            }
        }
        if (rightCloseContainer !=null) {
            rightCloseContainer.setOnClickListener(view -> dismiss());
        }

    }

    public void setDialogWidth(int dialogWidth) {
        this.dialogWidth = dialogWidth;
    }

    public void setDialogHeight(int dialogHeight) {
        this.dialogHeight = dialogHeight;
    }

    public boolean isSingleBnt() {
        return isSingleBnt;
    }

    public SimpleDialog setSingleBnt(boolean singleBnt) {
        isSingleBnt = singleBnt;
        return this;
    }

    public View.OnClickListener getCancelListener() {
        return cancelListener;
    }

    public void setCancelListener(View.OnClickListener cancelListener) {
        this.cancelListener = cancelListener;
    }

    public View.OnClickListener getOkListener() {
        return okListener;
    }

    public void setOkListener(View.OnClickListener okListener) {
        this.okListener = okListener;
    }

    public CharSequence getText() {
        return message;
    }

    public SimpleDialog setText(CharSequence text) {
        this.message = text;
        return this;
    }

    public SimpleDialog setLeftBtnText(CharSequence text) {
        this.leftText = text;
        return this;
    }

    public SimpleDialog setLeftBtnTextColor(int color) {
        this.mBtnLeftTextColor = color;
        return this;
    }

    public SimpleDialog setRightBtnText(CharSequence text) {
        this.rightText = text;
        return this;
    }

    public SimpleDialog setRightBtnTextColor(int color) {
        this.mBtnRightTextColor = color;
        return this;
    }

    public SimpleDialog setCenterBtnTextColor(int color) {
        this.mBtnCenterTextColor = color;
        return this;
    }

    public SimpleDialog setCenterTextStyle(int style) {
        this.mBtnCenterTextStyle = style;
        return this;
    }

    public SimpleDialog setCenterBtnText(CharSequence text) {
        this.centerBtnText = text;
        return this;
    }

    public SimpleDialog setTitleText(CharSequence text) {
        this.title = text;
        return this;
    }

    public SimpleDialog setMessageTextSize(int size) {
        mMessageTextSize = size;
        return this;
    }

    public SimpleDialog setMessageTextColor(int color) {
        mMessageTextColor = color;
        return this;
    }

    public SimpleDialog setTitleGravity(int gravity) {
        mTitleGravity = gravity;
        return this;
    }

    public SimpleDialog setTextBold(boolean isBold) {
        mTextBold = isBold;
        return this;
    }

    public SimpleDialog setMessageGravity(int gravity) {
        mMessageGravity = gravity;
        return this;
    }

    public SimpleDialog setTextMessageViewMaxLines(int lines) {
        mMessageTextLines = lines;
        return this;
    }

    public TextView getTextMessageView() {
        return textMessageView;
    }
}
